// Ovi rendering API is available at the following url
// https://projects.forum.nokia.com/ovimapsrenderingapi
// http://api.maps.nokia.com/en/restmaps/api.html
// this file is a modification of the original available here:
// http://www.developer.nokia.com/Community/Wiki/Using_the_Map_Image_API_in_QML

import QtQuick 1.0

Item {
    id: mapTile

    property real latitude
    property real longitude
    property int zoom: 16
    property bool loadUrl

    property bool newLocation: false

    function managZoom() {
        setNewLocation();
        loadMap();
    }

    function setNewLocation()
    {
        mapTile.newLocation = true;
    }

    function loadMap()
    {

        if (myWin.statesMan.pageview_map && mapTile.loadUrl && mapTile.setNewLocation) {
//            console.log("cambio");
//            mapImage.source = 'http://m.ovi.me/?c=' + latitude + ',' + longitude +
            mapImage.source = 'http://m.nok.it/?c=' + latitude + ',' + longitude +
                    '&z=' + zoom +
                    '&w=' + mapImage.width + '&h=' + mapImage.height +
                    '&nord';
//            console.log(mapImage.source);
        }
//        else {
//            console.log("salto");
//        }
    }

    Image {
        id: mapImage
        anchors.fill: parent

        Column {
            anchors.right: parent.right
            anchors.bottom: parent.bottom
            anchors.margins: 15
            spacing: 15

            BtnMapZoom {
                zoomIn: true

            }
            BtnMapZoom {
                zoomIn: false

            }
        }



    }
    Text {
        id: errorText
        anchors.centerIn: parent
        text: "Error loading the map"
        color: myWin.theme.passaggiview_loading_text_color
    }

    Column
    {
        id: loadingView
        anchors.centerIn: parent

        AnimatedImage {
            source: "qrc:ajax-loader.gif"
            anchors.horizontalCenter: parent.horizontalCenter
        }

        Text {
            text: "Loading map..."
            color: "white"
        }
    }

    Component.onCompleted: {

        mapTile.zoomChanged.connect(setNewLocation)
        mapTile.latitudeChanged.connect(setNewLocation)
        mapTile.longitudeChanged.connect(setNewLocation)
        mapTile.loadUrlChanged.connect(loadMap)

        //        loadMap()
    }

    states: [
        State {
            name: "image"
            when: mapImage.status == Image.Ready
            PropertyChanges { target: loadingView; visible: false}
            PropertyChanges { target: mapImage; visible: true}
            PropertyChanges { target: errorText; visible: false}
        },
        State {
            name: "loading"
            when: (!(mapImage.status == Image.Ready)) && (!(mapImage.status == Image.Error))
            PropertyChanges { target: loadingView; visible: true}
            PropertyChanges { target: mapImage; visible: false}
            PropertyChanges { target: errorText; visible: false}
        },
        State {
            name: "error"
            when: mapImage.status == Image.Error
            PropertyChanges { target: loadingView; visible: false}
            PropertyChanges { target: mapImage; visible: false}
            PropertyChanges { target: errorText; visible: true}
        }

    ]
}
